﻿#include "Linkspyder.h"
#include "CSVTool.h"
#include "RETool.h"
#include "Setting.h"
#include <QVector>
Linkspyder::Linkspyder(QObject* parent)
    : Spyder(parent)
{}
Linkspyder::~Linkspyder()
{
    Settings::GetInstance().SyncSettings();
}
QVector<QString> Linkspyder::Decode(const QByteArray& data)
{
    QVariant regularExpressContent = Settings::GetInstance().Getvalue("RegularExpression");
    if (regularExpressContent.isValid()) {
        QString reContent = regularExpressContent.toString();
        Logger::GetInstance().LogInfo("web page content retrieved from settings: " + reContent);
    }
    else {
        Logger::GetInstance().LogError("Failed to retrieved web page content from settings!");
    }
    RETool           reTool(regularExpressContent.toString());
    QString          htmlContent  = QString::fromUtf8(data);
    QVector<QString> matchResults = reTool.Match(htmlContent);
    return matchResults;
}
void Linkspyder::DealWithInfo(QVector<QString> info)
{
    if (info.size() == 0) { return; }
    CSVTool          csvTool("spyder.txt");
    QVector<QString> header{"title:", "url"};
    csvTool.WriteCSV(header, info, "utf-8");
    for (auto& item : info) { Logger::GetInstance().LogInfo(item); }
}